import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

public class Reapper {

    static {
        // 配置 Log4j 将日志输出到当前目录下的文件
        String configuration =
                "<Configuration status=\"error\">" +
                        "<Appenders>" +
                        "<File name=\"File\" fileName=\"./mylogfile.log\">" +
                        "<PatternLayout pattern=\"%d %p %c{1.} [%t] %m%n\" />" +
                        "</File>" +
                        "</Appenders>" +
                        "<Loggers>" +
                        "<Root level=\"info\">" +
                        "<AppenderRef ref=\"File\" />" +
                        "</Root>" +
                        "</Loggers>" +
                        "</Configuration>";

        ConfigurationSource source = null;
        try {
            source = new ConfigurationSource(new ByteArrayInputStream(configuration.getBytes()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Configurator.initialize(null, source);
    }

    private static final Logger logger = LogManager.getLogger(Reapper.class);

    public static void main(String[] args) {

        Connection connection = null;
        logger.info("开始。。。。。。。。。。。。。。。。。");
        try {
            long maxTime = 0;
            long minTime = 0;
            String dbFile = "E:/data/WeChatWork/WeChat Files/yb2133/FileStorage/File/2024-07/8/logs/accessrecords.db";
            if (args.length > 0) {
                dbFile = args[0];
                minTime = Long.parseLong(args[1]);
                maxTime = Long.parseLong(args[2]);

            }
            Class.forName("org.sqlite.JDBC");



            // 连接到SQLite数据库文件
            connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile);

            // 创建Statement用于执行SQL语句
            Statement statement = connection.createStatement();

            String sql = "SELECT * FROM accessRecords  WHERE 1=1 ";
            if (maxTime>0){
                sql+=" and accessTime < "+maxTime ;
            }
            if (minTime>0){
                sql+=" and accessTime > "+minTime ;
            }
            // 执行查询并获取结果
            ResultSet resultSet = statement.executeQuery(sql);

            // 处理结果
            while (resultSet.next()) {



                // 获取并打印每一列的值
                String url = resultSet.getString("requestURL");
                String queryString = resultSet.getString("queryString");

//                System.out.println(resultSet.getString("queryString"));
                System.out.println(url);
                System.out.println(queryString);

                logger.info(url);
                logger.info(queryString);

                request(url+"?"+queryString);
//                request("http://localhost:8090/iserver/services/3D-local3DCache-TF_PAPN_MANHOLE_BDataSource/rest/realspace/");
            }

//
//
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    // 关闭连接
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }



    public static void request(String url) {
        String requestMethod = "GET"; // 请求方式，如 GET 或 POST
//        Map<String, String> parameters = new HashMap<>(); // 请求参数
//
//        // 设置请求参数
//        parameters.put("param1", "value1");
//        parameters.put("param2", "value2");
        try {
            URL apiUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
            connection.setRequestMethod(requestMethod);
            connection.setDoOutput(true);

            // 发送请求参数
//            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
//            for (Map.Entry<String, String> entry : parameters.entrySet()) {
//                writer.write(entry.getKey() + "=" + entry.getValue() + "&");
//            }
//            writer.flush();

            // 读取响应
//            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//            String line;
//            StringBuffer response = new StringBuffer();
//            while ((line = reader.readLine()) != null) {
//                response.append(line);
//            }
//            reader.close();
//
//            System.out.println("HTTP Response: " + response.toString());
            System.out.println("HTTP ResponseCode: " + connection.getResponseCode());
            logger.info("HTTP ResponseCode: " + connection.getResponseCode());

            connection.disconnect();
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }
}